<?php
include "./include/om_config.inc";

$smarty = new SmartyWWW();
$filePath = "./bhavcopiesfo";
$handle = '';
$monthOk = '';
$monthInt= '';
$monthIntExp= '';
$expiryDate2 = '';
$expiryDateBc = '';
$generalf_oQuery = "SELECT * FROM generalf_o";
$generalf_oResult = mysql_query($generalf_oQuery);
if($generalf_oRow = mysql_fetch_array($generalf_oResult))
{
  $fileName = $generalf_oRow['fileName'];
  $year  = substr($generalf_oRow['fileName'],7,4);
  $month = substr($generalf_oRow['fileName'],4,3);
  $date  = substr($generalf_oRow['fileName'],2,2);
  switch ($month)
  {
    case "JAN":
      $monthOk = '01';
      break;
    case "FEB":
      $monthOk = '02';
      break;
    case "MAR":
      $monthOk = '03';
      break;
    case "APR":
      $monthOk = '04';
      break;
    case "MAY":
      $monthOk = '05';
      break;
    case "JUN":
      $monthOk = '06';
      break;
    case "JUL":
      $monthOk = '07';
      break;
    case "AUG":
      $monthOk = '08';
      break;
    case "SEP":
      $monthOk = '09';
      break;
    case "OCT":
      $monthOk = '10';
      break;
    case "NOV":
      $monthOk = '11';
      break;
    case "DEC":
      $monthOk = '12';
      break;
  }

  $sDate = $year."-".$monthOk."-".$date;

  //Check that data already in bhavcopyf_o table :Start
  $storedbhavf_oQuery = "SELECT * FROM storedbhavf_o
                         WHERE storDate = '".$sDate."'";
  $storedbhavf_oResult = mysql_query($storedbhavf_oQuery);
  if($storedbhavf_oRow = mysql_fetch_array($storedbhavf_oResult))
  {
    if($storedbhavf_oRow['status'] == "Ok")
      $varStoreBhav = false;
    else
    {
      $bhavcopyf_oDelete = "DELETE FROM bhavcopyf_o WHERE bhavcopyDate = '".$sDate."'";
      $bhavcopyf_oDeleteResult = mysql_query($bhavcopyf_oDelete);
      $varStoreBhav = true;
    }
  }
  else
  {
    $insertStor = "INSERT INTO storedbhavf_o(storDate,status)
                  VALUES('".$sDate."','started')";
    $insertStorResult = mysql_query($insertStor);
    $varStoreBhav = true;
  }
  //Check that data already in bhavcopyf_o table :End

  if($varStoreBhav = true)
  {
    $row = 0;
    $scLineCount = 1;
    $FOLineCount = 1;
    $NSELineCount = 1;
    $handle = fopen($filePath."/".$fileName, "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
      if($row > 0)
      {
        $num = count($data);

        $exchange     = 'F_O';
        $expiryDate   = $data[2];
        $bhavcopyDate = $data[14];
        $contractCode = $data[1];
        $openPrice    = $data[5];
        $highPrice    = $data[6];
        $lowPrice     = $data[7];
        $closePrice   = $data[8];
        $openInterest = $data[12];
        $disc207 = strpos($data[2],'-',0);
        //$pose = strpos($data[14],'-',0) == 1;
        if($disc207 == 1)
        {
          $monthStree = substr($data[2],2,3);
        }
        else if($disc207 == 2)
        {
          $monthStree = substr($data[2],3,3);
        }
        else
        {
        }
        $monthStr = $monthStree;

        switch ($monthStr)
        {
          case "Jan":
            $monthInt = '01';
            break;
          case "Feb":
            $monthInt = '02';
            break;
          case "Mar":
            $monthInt = '03';
            break;
          case "Apr":
            $monthInt = '04';
            break;
          case "May":
            $monthInt = '05';
            break;
          case "Jun":
            $monthInt = '06';
            break;
          case "Jul":
            $monthInt = '07';
            break;
          case "Aug":
            $monthInt = '08';
            break;
          case "Sep":
            $monthInt = '09';
            break;
          case "Oct":
            $monthInt = '10';
            break;
          case "Nov":
            $monthInt = '11';
            break;
          case "Dec":
            $monthInt = '12';
            break;
        }
        //echo '<br>'.strpos($data[14],'-',0);
        $disc = strpos($data[2],'-',0);
        if($disc == 1)
        {
          $yearDub2 = "20".substr($data[2],8,2);
          $expiryDate2   = $yearDub2."-".$monthInt."-0".substr($data[2],0,1);
          $expiryDateBc = $yearDub2."".$monthStr."0".substr($data[2],0,1);
        }
        else if($disc == 2)
        {
          $yearDub23 = substr($data[2],7,4);
          $expiryDate2   = $yearDub23."-".$monthInt."-".substr($data[2],0,2);
          $expiryDateBc = $yearDub23."".$monthStr."".substr($data[2],0,2);
        }
        else
        {
        }
        ////////For Expiry:Start
        if(strlen(trim($contractCode)) > 0)
        {
          $disc211 = strpos($data[14],'-',0);
          //$pose = strpos($data[14],'-',0) == 1;
          if($disc211 == 1)
          {
            $monthStrExpee = substr($data[14],2,3);
          }
          else if($disc211 == 2)
          {
            $monthStrExpee = substr($data[14],3,3);
          }
          $monthStrExp = $monthStrExpee;
          switch ($monthStrExp)
          {
            case "JAN":
              $monthIntExp = '01';
              break;
            case "FEB":
              $monthIntExp = '02';
              break;
            case "Mar":
              $monthIntExp = '03';
              break;
            case "APR":
              $monthIntExp = '04';
              break;
            case "MAY":
              $monthIntExp = '05';
              break;
            case "JUN":
              $monthIntExp = '06';
              break;
            case "JUL":
              $monthIntExp = '07';
              break;
            case "AUG":
              $monthIntExp = '08';
              break;
            case "SEP":
              $monthIntExp = '09';
              break;
            case "OCT":
              $monthIntExp = '10';
              break;
            case "NOV":
              $monthIntExp = '11';
              break;
            case "DEC":
              $monthIntExp = '12';
              break;
          }

          $disc2 = strpos($data[14],'-',0);
          //$pose = strpos($data[14],'-',0) == 1;
          //echo '<BR>'.substr($data[14],0,1);
          if($disc2 == 1)
          {
            $yearDub = "20".substr($data[14],8,2);
            $bhavcopyDate2 = $yearDub."-".$monthIntExp."-0".substr($data[14],0,1);
          }
          else if($disc2 == 2)
          {
            $yearDub = substr($data[14],7,4);
            $bhavcopyDate2 = $yearDub."-".$monthIntExp."-".substr($data[14],0,2);
          }
          else
          {
          }
        }
        $insertDenied=0;
        $noDuplication ="SELECT contractCode FROM bhavcopyf_o WHERE bhavcopyDate='".$bhavcopyDate2."'
                         AND contractCode='".$contractCode."' AND
                         expiryDate='".$expiryDate2."'";
        $resultNoDuplicate = mysql_query($noDuplication);
        if($resultNoDuplicate && mysql_num_rows($resultNoDuplicate)!=0)
          $insertDenied = '1';
        if($highPrice == 0 && $openPrice == 0 && $lowPrice == 0 && $closePrice == 0 && $openInterest == 0)
          $insertDenied = '1';
        if($insertDenied == 0)
        {
          if($data[4] == 'XX')
          {
            $insertData = "INSERT INTO bhavcopyf_o (exchange,bhavcopyDate,contractCode,
                                                    expiryDate,expiryDateBc,openPrice,highPrice,lowPrice,closePrice,openInterest)
                           VALUES ('".$exchange."','".$sDate."','".$contractCode."',
                                   '".$expiryDate2."','".$expiryDateBc."',
                                   ".$openPrice.",".$highPrice.",".$lowPrice.",".$closePrice.",".$openInterest.")";
            $resultData = mysql_query($insertData);
          }
        }
      }
      $row++;
      if($data[0] == "OPTIDX" || $data[0] == "OPTSTK")
        break;
    }
    $updateStorK = "UPDATE storedbhavf_o SET status = 'Ok'
                    WHERE storDate = '".$sDate."'";
    $updateStorResultK = mysql_query($updateStorK);
    fclose($handle);
  }
header("Location: ./txtFileF_O.php");
}
?>